home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / flowlayo.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  6.4 KB  |  230 lines

  1. /*
  2.  * @(#)FlowLayout.java    1.16 95/09/08 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. /**
  22.  * Flow layout is used to layout buttons in a panel. It will arrange
  23.  * buttons left to right until no more buttons fit on the same line.
  24.  * Each line is centered.
  25.  *
  26.  * @version     1.16, 09/08/95
  27.  * @author     Arthur van Hoff
  28.  * @author     Sami Shaio
  29.  */
  30. public class FlowLayout implements LayoutManager {
  31.  
  32.     /**
  33.      * The left alignment variable. 
  34.      */
  35.     public static final int LEFT     = 0;
  36.  
  37.     /**
  38.      * The right alignment variable. 
  39.      */
  40.     public static final int CENTER     = 1;
  41.  
  42.     /**
  43.      * The right alignment variable.
  44.      */
  45.     public static final int RIGHT     = 2;
  46.  
  47.     int align;
  48.     int hgap;
  49.     int vgap;
  50.  
  51.     /**
  52.      * Constructs a new Flow Layout with a centered alignment.
  53.      */
  54.     public FlowLayout() {
  55.     this(CENTER, 5, 5);
  56.     }
  57.  
  58.     /**
  59.      * Constructs a new Flow Layout with the specified alignment.
  60.      * @param align the alignment value
  61.      */
  62.     public FlowLayout(int align) {
  63.     this(align, 5, 5);
  64.     }
  65.  
  66.     /**
  67.      * Constructs a new Flow Layout with the specified alignment and gap
  68.      * values.
  69.      * @param align the alignment value
  70.      * @param hgap the horizontal gap variable
  71.      * @param vgap the vertical gap variable
  72.      */
  73.     public FlowLayout(int align, int hgap, int vgap) {
  74.     this.align = align;
  75.     this.hgap = hgap;
  76.     this.vgap = vgap;
  77.     }
  78.  
  79.     /**
  80.      * Adds the specified component to the layout. Does not apply.
  81.      * @param name the name of the component
  82.      * @param comp the the component to be added
  83.      */
  84.     public void addLayoutComponent(String name, Component comp) {
  85.     }
  86.  
  87.     /**
  88.      * Removes the specified component from the layout. Does not apply.
  89.      * @param comp the component to remove
  90.      */
  91.     public void removeLayoutComponent(Component comp) {
  92.     }
  93.  
  94.     /**
  95.      * Returns the preferred dimensions for this layout given the components
  96.      * in the specified target container.
  97.      * @param target the component which needs to be laid out
  98.      * @see Container
  99.      * @see #minimumSize
  100.      */
  101.     public Dimension preferredLayoutSize(Container target) {
  102.     Dimension dim = new Dimension(0, 0);
  103.     int nmembers = target.countComponents();
  104.  
  105.     for (int i = 0 ; i < nmembers ; i++) {
  106.         Component m = target.getComponent(i);
  107.         if (m.visible) {
  108.         Dimension d = m.preferredSize();
  109.         dim.height = Math.max(dim.height, d.height);
  110.         if (i > 0) {
  111.             dim.width += hgap;
  112.         }
  113.         dim.width += d.width;
  114.         }
  115.     }
  116.     Insets insets = target.insets();
  117.     dim.width += insets.left + insets.right + hgap*2;
  118.     dim.height += insets.top + insets.bottom + vgap*2;
  119.     return dim;
  120.     }
  121.  
  122.     /**
  123.      * Returns the minimum dimensions needed to layout the components
  124.      * contained in the specified target container.
  125.      * @param target the component which needs to be laid out 
  126.      * @see #preferredSize
  127.      */
  128.     public Dimension minimumLayoutSize(Container target) {
  129.     Dimension dim = new Dimension(0, 0);
  130.     int nmembers = target.countComponents();
  131.  
  132.     for (int i = 0 ; i < nmembers ; i++) {
  133.         Component m = target.getComponent(i);
  134.         if (m.visible) {
  135.         Dimension d = m.minimumSize();
  136.         dim.height = Math.max(dim.height, d.height);
  137.         if (i > 0) {
  138.             dim.width += hgap;
  139.         }
  140.         dim.width += d.width;
  141.         }
  142.     }
  143.     Insets insets = target.insets();
  144.     dim.width += insets.left + insets.right + hgap*2;
  145.     dim.height += insets.top + insets.bottom + vgap*2;
  146.     return dim;
  147.     }
  148.  
  149.     /** 
  150.      * Centers the elements in the specified row, if there is any slack.
  151.      * @param target the component which needs to be moved
  152.      * @param x the x coordinate
  153.      * @param y the y coordinate
  154.      * @param width the width dimensions
  155.      * @param height the height dimensions
  156.      * @param rowStart the beginning of the row
  157.      * @param rowEnd the the ending of the row
  158.      */
  159.     private void moveComponents(Container target, int x, int y, int width, int height, int rowStart, int rowEnd) {
  160.     switch (align) {
  161.     case LEFT:
  162.         break;
  163.     case CENTER:
  164.         x += width / 2;
  165.         break;
  166.     case RIGHT:
  167.         x += width;
  168.         break;
  169.     }
  170.     for (int i = rowStart ; i < rowEnd ; i++) {
  171.         Component m = target.getComponent(i);
  172.         if (m.visible) {
  173.         m.move(x, y + (height - m.height) / 2);
  174.         x += hgap + m.width;
  175.         }
  176.     }
  177.     }
  178.  
  179.     /**
  180.      * Lays out the container. This method will actually reshape the
  181.      * components in target in order to satisfy the constraints of
  182.      * the BorderLayout object. 
  183.      * @param target the specified component being laid out.
  184.      * @see Container
  185.      */
  186.     public void layoutContainer(Container target) {
  187.     Insets insets = target.insets();
  188.     int maxwidth = target.width - (insets.left + insets.right + hgap*2);
  189.     int nmembers = target.countComponents();
  190.     int x = 0, y = insets.top + vgap;
  191.     int rowh = 0, start = 0;
  192.  
  193.     for (int i = 0 ; i < nmembers ; i++) {
  194.         Component m = target.getComponent(i);
  195.         if (m.visible) {
  196.         Dimension d = m.preferredSize();
  197.         m.resize(d.width, d.height);
  198.     
  199.         if ((x == 0) || ((x + d.width) <= maxwidth)) {
  200.             if (x > 0) {
  201.             x += hgap;
  202.             }
  203.             x += d.width;
  204.             rowh = Math.max(rowh, d.height);
  205.         } else {
  206.             moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, i);
  207.             x = d.width;
  208.             y += vgap + rowh;
  209.             rowh = d.height;
  210.             start = i;
  211.         }
  212.         }
  213.     }
  214.     moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, nmembers);
  215.     }
  216.     
  217.     /**
  218.      * Returns the String representation of this FlowLayout's values.
  219.      */
  220.     public String toString() {
  221.     String str = "";
  222.     switch (align) {
  223.       case LEFT:    str = ",align=left"; break;
  224.       case CENTER:  str = ",align=center"; break;
  225.       case RIGHT:   str = ",align=right"; break;
  226.     }
  227.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]";
  228.     }
  229. }
  230.